Unlike many popular programming languages, if and when in Kotlin are expressions, not statements. In other words, they evaluate to a value.

In the case of if, the value is whatever the if branch evaluates to (if the comparison expression was true) or whatever the else branch evaluates to (if the comparison expression was false).

In this case, we need the if to be "exhaustive", covering all possible scenarios. That just means that the else is required, to provide the value to return if the comparison expression evaluates to false.

You can learn more about this in:
Run Expand Edit